package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.commen.response.JsonResult;
import cn.tedu._05weibo.commen.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDTO;
import cn.tedu._05weibo.pojo.entity.Weibo;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._05weibo.pojo.vo.WeiboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Max;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "微博模块")
@Validated
@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    /**
     * 1.发布微博功能
     * 接口地址: /v1/weibo/insert
     * 请求方式: POST
     * 请求数据类型: application/json
     * 响应数据类型: int 2->未登录 1->发布成功
     */
    @ApiOperation(value = "1.发布微博")
    @PostMapping("/insert")
    public JsonResult insertWeibo(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.debug("weiboDTO"+weiboDTO);
        //验证登录状态
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null)
//            return new JsonResult(StatusCode.NOT_LOGIN);
        //发布微博
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weibo);
        return JsonResult.success();
    }

    /**
     * 2.微博首页列表功能
     * 接口地址: /v1/weibo/selectIndex
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: List<WeiboVO>
     */
    @ApiOperation(value = "2.微博首页列表")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex(){
        //查询微博内容并存入集合列表
        List<WeiboVO> weiboVOS = weiboMapper.selectIndex();
        return JsonResult.success(weiboVOS);
    }

    /**
     * 3.微博详情页功能
     * 接口地址: /v1/weibo/selectById
     * 请求方式: GET
     * 请求数据类型: application/x-www-form-urlencoded
     * 响应数据类型: WeiboDetailVO
     */
    @ApiOperation(value = "3.微博详情页")
    @ApiImplicitParam(name = "id",value = "微博编号",required = true,example = "200",dataType = "int")
    @GetMapping("/selectById")
    public JsonResult selectById(@RequestParam @Range(min = 200,max = 99999999,message = "微博编号在200-99999999之间") int id){
        if (id<0)
            throw new IllegalArgumentException("非法的id值");
        //查询微博详情并返回客户端
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.success(weiboDetailVO);
    }
}
